package com.example.mwavlearndemo.av.data.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatButton;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mwavlearndemo.R;
import com.example.mwavlearndemo.av.data.bean.Function;

import java.util.List;

public class FunctionAdapter extends RecyclerView.Adapter<FunctionAdapter.ViewHolder>{
    private static final String TAG = "FunctionAdapter";
    private Context context;
    private final List<Function> functionList;
    public FunctionAdapter(List<Function> functionList){
        this.functionList = functionList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
       if (context == null){
           context = parent.getContext();
       }
        View view = LayoutInflater.from(context).inflate(R.layout.item_function,
                parent,false);
       ViewHolder viewHolder = new ViewHolder(view);
       viewHolder.appCompatButton.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               int position = viewHolder.getAdapterPosition();
               Function function = functionList.get(position);
               context.startActivity(new Intent(context,function.getCls()));
           }
       });
       return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Function function = functionList.get(position);
        holder.appCompatButton.setText(function.getName());
    }

    @Override
    public int getItemCount() {
        return functionList.size();
    }


    static class ViewHolder extends RecyclerView.ViewHolder{
        AppCompatButton appCompatButton;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            appCompatButton = itemView.findViewById(R.id.btnEnter);
        }
    }
}
